//
//  RLoginViewController.m
//  Riji
//
//  Created by liujianlong on 2022/8/17.
//

#import "RLoginViewController.h"
#import "RInput.h"
#import "SWPrivacyViewController.h"
@interface RLoginViewController ()
@property (weak, nonatomic) IBOutlet RInput *name;
@property (weak, nonatomic) IBOutlet RInput *psw;
@property (weak, nonatomic) IBOutlet RInput *psw2;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UIButton *privacyBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnTop;
@property (weak, nonatomic) IBOutlet UIButton *selectBtn;
@property (strong, nonatomic) IBOutlet UIView *bar;
@end

@implementation RLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI
{
    if (self.type == RLoginTypeRegister) {
        self.title = LS(@"注册");
        self.name.placeholder = LS(@"请输入用户名");
        self.psw.placeholder = LS(@"请输入密码");
        self.psw2.placeholder = LS(@"请再次输入密码");
        self.btnTop.constant = 120;
    }else if (self.type == RLoginTypeChange){
        self.title = LS(@"修改密码");
        self.name.placeholder = LS(@"请输入原密码");
        self.psw.placeholder = LS(@"请设置新密码");
        self.psw2.placeholder = LS(@"请再次输入新密码");
        self.bar.hidden = YES;
        self.btnTop.constant = 120;
        [self.btn setTitle:LS(@"确定") forState:UIControlStateNormal];
    }else if (self.type == RLoginTypeLogin){
        self.title = LS(@"登录");
        self.name.placeholder = LS(@"请输入用户名");
        self.psw.placeholder = LS(@"请输入密码");
        self.name.btn.hidden = YES;
        self.psw2.hidden = YES;
        self.btnTop.constant = 40;
        UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, StuBarHight + 5, 40, 40)];
        [rightBtn setTitle:LS(@"注册") forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(actionForRightBtn:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *baritem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        self.navigationItem.rightBarButtonItem = baritem;
    }
}
#pragma mark - 判断输入框内容是否合法
- (BOOL)judge{
    if (self.name.input.text.length <= 0) {
        [RLHud msg:self.type == RLoginTypeChange ? LS(@"原密码不能为空") : LS(@"用户名不能为空")];
        return NO;
    }else if (self.psw.input.text.length <= 0){
        [RLHud msg:LS(@"密码不能为空")];
        return NO;
    }else if (self.psw2.input.text.length <= 0 && (self.type == RLoginTypeRegister || self.type == RLoginTypeChange)){
        [RLHud msg:LS(@"请确认密码")];
        return NO;
    }else if ([self.psw2.input.text isEqualToString:self.psw.input.text] == NO && (self.type == RLoginTypeRegister || self.type == RLoginTypeChange)){
        [RLHud msg:LS(@"两次密码不一致")];
        return NO;
    }else if (![self.name.input.text isNumberOrAlphabet]){
        [RLHud msg:LS(@"用户名只能包含数字和字母")];
    }else if (![self.psw.input.text isNumberOrAlphabet] || ![self.psw.input.text isNumberOrAlphabet]){
        [RLHud msg:LS(@"密码只能包含数字和字母")];
    }
    return YES;
}
- (void)http:(NSDictionary *)dict api:(NSString*)api
{
    [CCHttp post:api para:dict success:^(id  _Nonnull response) {
        if ([api isEqualToString:api_login]) {
            RUserModel *user = [[RUserModel alloc] init];
            user.token = [response valueForKey:@"data"];
            APP.user = user;
            [self.navigationController popToRootViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:notifyNoteUpdate object:nil];
        }else {
            NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:dict];
            [dict2 setValue:@"" forKey:@"code"];
            [self http:dict2 api:api_login];
        }
    } fail:^(NSError * _Nonnull error) { }];
}
#pragma mark - 切换注册/登陆页面
- (void)actionForRightBtn:(UIButton*)btn
{
    RLoginViewController *vc = [RLoginViewController new];
    vc.type = RLoginTypeRegister;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 登录注册
- (IBAction)actionForEnter:(id)sender {
    if ([self judge] == NO) { return;}
    if (self.selectBtn.selected == NO && self.type != RLoginTypeChange) {
        return [RLHud msg:LS(@"请仔细阅读相关协议和条款")];
    }
    if (self.type == RLoginTypeRegister) {
        [self http:@{@"userName":self.name.input.text,@"password":self.psw.input.text} api:api_register];
    }else if (self.type == RLoginTypeLogin){
        [self http:@{@"userName":self.name.input.text,@"password":self.psw.input.text,@"code":@""} api:api_login];
    }else if (self.type == RLoginTypeChange){
        NSString *api = [NSString stringWithFormat:@"%@?data=%@",api_updatePSW,self.psw.input.text];
        RLoginViewController *vc = [RLoginViewController new];
        vc.type = RLoginTypeLogin;
        vc.backBtn.hidden = YES;
        [self.navigationController pushViewController:vc animated:NO];
        [[NSNotificationCenter defaultCenter] postNotificationName:notifyNoteUpdate object:nil];
        [CCHttp get:api para:nil header:nil success:^(id  _Nonnull response) {
            NSLog(@"");
        } fail:^(NSError * _Nonnull error) {}];
    }
}
#pragma mark - 阅读隐私/协议
- (IBAction)actionForPrivacy:(id)sender {
    SWPrivacyViewController *vc   = [SWPrivacyViewController new];
    vc.title = LS(@"隐私政策");
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)actionForSelectPrivacy:(UIButton *)sender {
    sender.selected = !sender.selected;
}

@end
